package com.thinkcoder.util.json;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;

import java.io.IOException;
import java.util.List;
import java.util.Map;


/**
 * 使用jackson写的json工具类
 *
 * @author 商俊帅
 * @date 2021/04/16 15:23
 **/
@Slf4j
public class JsonUtil {

    private static final ObjectMapper mapper = new ObjectMapper();

    /**
     * 对象转json字符串
     *
     * @param obj
     * @return
     */
    public static String ObjectToJsonStr(Object obj){

        if(obj == null) return null;
        if(obj.getClass()==String.class) return (String) obj;

        try {
            return mapper.writeValueAsString(obj);
        } catch (JsonProcessingException e) {
            log.error("json序列化出错"+obj,e);
            return null;
        }
    }

    /**
     * java对象转换为json对象
     */
    public static ObjectNode beanToJsonObject(Object obj){
        JsonNode jsonNode = mapper.valueToTree(obj);
        return (ObjectNode) jsonNode;
    }

    //----------------------------------json转各种对象-----------------------------------

    /***
     * json转java对象
     */
    public static <T> T jsonStrToBean(String json,Class<T> clazz){

        try {
            return mapper.readValue(json,clazz);
        } catch (JsonProcessingException e) {
            log.error("json解析出错：" + json, e);
            return null;
        }
    }

    /***
     * json字符串转list对象
     */
    public static <E> List<E> jsonStrToList(String json,Class<E> clazz){

        try {
            return mapper.readValue(json,mapper.getTypeFactory().constructCollectionType(List.class,clazz));
        } catch (JsonProcessingException e) {
            log.error("json转list类型错误"+json,e);
            return null;
        }

    }


    /***
     * json字符串转map对象
     */
    public static <K, V> Map<K, V> jsonStrToMap(String json, Class<K> kClass, Class<V> vClass) {
        try {
            return mapper.readValue(json, mapper.getTypeFactory().constructMapType(Map.class, kClass, vClass));
        } catch (IOException e) {
            log.error("json解析出错：" + json, e);
            return null;
        }
    }

    /**
     * json字符串转任意类型
     */
    public static <T> T nativeRead(String json, TypeReference<T> type) {
        try {
            return mapper.readValue(json, type);
        } catch (IOException e) {
            log.error("json解析出错：" + json, e);
            return null;
        }
    }

    @Data
    static class User{
        private String name;
        private int age;
    }

    public static void main(String[] args) {

        User user = new User();
        user.setAge(1);
        user.setName("21");
        //对象转json字符串
        System.out.println(ObjectToJsonStr(user));

        String json = "{\"name\":\"21\",\"age\":1}";
        User user1 = jsonStrToBean(json, User.class);
        System.out.println(user1);

        ObjectNode jsonNodes = beanToJsonObject(user);
        jsonNodes.get("name");
        System.out.println(jsonNodes+"，得到某个属性"+jsonNodes.get("age"));
    }

}
